<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @reset="handleReset" @submit="handleSubmit">
      <template #dateSelect="{ model, field }">
        <n-date-picker
          v-model:formatted-value="model[field]"
          clearable
          type="daterange"
          value-format="yyyy-MM-dd"
        />
      </template>
    </BasicForm>

    <BasicTable ref="actionRef" :columns="columns" :request="loadDataTable" :scroll-x="2048" />
  </n-card>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { getAnchorRegistData } from '@/api/statistics/daily';
  import { formatToDate } from '@/utils/dateUtil';

  const schemas: FormSchema[] = [
    {
      field: 'chanName',
      component: 'NInput',
      label: '渠道名称',
      componentProps: {
        placeholder: '请输入完整渠道名称',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'timeRange',
      label: '日期',
      componentProps: {
        placeholder: '日期',
      },
      slot: 'dateSelect',
    },
  ];
  const router = useRouter();
  const formRef: any = ref(null);
  const message = useMessage();
  const dialog = useDialog();
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    chanName: '',
    timeRange: [],
  });

  const params = ref({});

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  const loadDataTable = async (res) => {
    const param = { ...formParams, ...res };
    console.log(param);
    const data = await getAnchorRegistData(param);
    console.log(data);
    return data;
  };

  function reloadTable() {
    actionRef.value.reload();
  }

  // function createSummary() {}

  function handleSubmit(values: Recordable) {
    console.log('submit', values);
    let tempParams = unref(formParams);
    tempParams = Object.assign(tempParams, values);
    if (values.makeDate) {
      let [startTime, endTime]: [number, number] = values.makeDate;
      let startTimeS = formatToDate(new Date(startTime));
      let endTimeS = formatToDate(new Date(endTime));
      tempParams = Object.assign(tempParams, { startTime: startTimeS, endTime: endTimeS });
    }
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }

  onMounted(async () => {
    // registerHandler(ItemTableSwitchActions);
    // const chanList = await getChanList();
    // registChanArray.value = chanList.map((chan) => {
    //   return {
    //     label: chan.chanName,
    //     value: chan.chanId
    //   }
    // });
  });
</script>

<style lang="less" scoped></style>
